﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using log4net;
using Telerik.WinControls.UI;
using System.IO;
using Telerik.WinControls.Data;
namespace QuanLyNhaSach
{
    public partial class frmHangBanTraLai : Form
    {
        public frmHangBanTraLai()
        {
            InitializeComponent();
        }

        //Tao Parttern Seleton
        static frmHangBanTraLai doituong = null;
        #region "Khai Bao Bien"
        private ILog logger;
        sv_QuanLyNhaSach.AdapterClient service = new sv_QuanLyNhaSach.AdapterClient();
        #endregion
        public static frmHangBanTraLai TaoDoiTuong()
        {
            if (doituong == null)
                doituong = new frmHangBanTraLai();
            return doituong;
        }
        #region "Ham chuyen so thanh chu"
        private static string Chu(string gNumber)
        {
            string result = "";
            switch (gNumber)
            {
                case "0":
                    result = "không";
                    break;
                case "1":
                    result = "một";
                    break;
                case "2":
                    result = "hai";
                    break;
                case "3":
                    result = "ba";
                    break;
                case "4":
                    result = "bốn";
                    break;
                case "5":
                    result = "năm";
                    break;
                case "6":
                    result = "sáu";
                    break;
                case "7":
                    result = "bảy";
                    break;
                case "8":
                    result = "tám";
                    break;
                case "9":
                    result = "chín";
                    break;
            }
            return result;
        }
        private static string Donvi(string so)
        {
            string Kdonvi = "";

            if (so.Equals("1"))
                Kdonvi = "";
            if (so.Equals("2"))
                Kdonvi = "nghìn";
            if (so.Equals("3"))
                Kdonvi = "triệu";
            if (so.Equals("4"))
                Kdonvi = "tỷ";
            if (so.Equals("5"))
                Kdonvi = "nghìn tỷ";
            if (so.Equals("6"))
                Kdonvi = "triệu tỷ";
            if (so.Equals("7"))
                Kdonvi = "tỷ tỷ";

            return Kdonvi;
        }
        private static string Tach(string tach3)
        {
            string Ktach = "";
            if (tach3.Equals("000"))
                return "";
            if (tach3.Length == 3)
            {
                string tr = tach3.Trim().Substring(0, 1).ToString().Trim();
                string ch = tach3.Trim().Substring(1, 1).ToString().Trim();
                string dv = tach3.Trim().Substring(2, 1).ToString().Trim();
                if (tr.Equals("0") && ch.Equals("0"))
                    Ktach = " không trăm lẻ " + Chu(dv.ToString().Trim()) + " ";
                if (!tr.Equals("0") && ch.Equals("0") && dv.Equals("0"))
                    Ktach = Chu(tr.ToString().Trim()).Trim() + " trăm ";
                if (!tr.Equals("0") && ch.Equals("0") && !dv.Equals("0"))
                    Ktach = Chu(tr.ToString().Trim()).Trim() + " trăm lẻ " + Chu(dv.Trim()).Trim() + " ";
                if (tr.Equals("0") && Convert.ToInt32(ch) > 1 && Convert.ToInt32(dv) > 0 && !dv.Equals("5"))
                    Ktach = " không trăm " + Chu(ch.Trim()).Trim() + " mươi " + Chu(dv.Trim()).Trim() + " ";
                if (tr.Equals("0") && Convert.ToInt32(ch) > 1 && dv.Equals("0"))
                    Ktach = " không trăm " + Chu(ch.Trim()).Trim() + " mươi ";
                if (tr.Equals("0") && Convert.ToInt32(ch) > 1 && dv.Equals("5"))
                    Ktach = " không trăm " + Chu(ch.Trim()).Trim() + " mươi lăm ";
                if (tr.Equals("0") && ch.Equals("1") && Convert.ToInt32(dv) > 0 && !dv.Equals("5"))
                    Ktach = " không trăm mười " + Chu(dv.Trim()).Trim() + " ";
                if (tr.Equals("0") && ch.Equals("1") && dv.Equals("0"))
                    Ktach = " không trăm mười ";
                if (tr.Equals("0") && ch.Equals("1") && dv.Equals("5"))
                    Ktach = " không trăm mười lăm ";
                if (Convert.ToInt32(tr) > 0 && Convert.ToInt32(ch) > 1 && Convert.ToInt32(dv) > 0 && !dv.Equals("5"))
                    Ktach = Chu(tr.Trim()).Trim() + " trăm " + Chu(ch.Trim()).Trim() + " mươi " + Chu(dv.Trim()).Trim() + " ";
                if (Convert.ToInt32(tr) > 0 && Convert.ToInt32(ch) > 1 && dv.Equals("0"))
                    Ktach = Chu(tr.Trim()).Trim() + " trăm " + Chu(ch.Trim()).Trim() + " mươi ";
                if (Convert.ToInt32(tr) > 0 && Convert.ToInt32(ch) > 1 && dv.Equals("5"))
                    Ktach = Chu(tr.Trim()).Trim() + " trăm " + Chu(ch.Trim()).Trim() + " mươi lăm ";
                if (Convert.ToInt32(tr) > 0 && ch.Equals("1") && Convert.ToInt32(dv) > 0 && !dv.Equals("5"))
                    Ktach = Chu(tr.Trim()).Trim() + " trăm mười " + Chu(dv.Trim()).Trim() + " ";

                if (Convert.ToInt32(tr) > 0 && ch.Equals("1") && dv.Equals("0"))
                    Ktach = Chu(tr.Trim()).Trim() + " trăm mười ";
                if (Convert.ToInt32(tr) > 0 && ch.Equals("1") && dv.Equals("5"))
                    Ktach = Chu(tr.Trim()).Trim() + " trăm mười lăm ";

            }


            return Ktach;

        }
        public static string So_chu(double gNum)
        {
            if (gNum == 0)
                return "Không đồng";

            string lso_chu = "";
            string tach_mod = "";
            string tach_conlai = "";
            double Num = Math.Round(gNum, 0);
            string gN = Convert.ToString(Num);
            int m = Convert.ToInt32(gN.Length / 3);
            int mod = gN.Length - m * 3;
            string dau = "[+]";

            // Dau [+ , - ]
            if (gNum < 0)
                dau = "[-]";
            dau = "";

            // Tach hang lon nhat
            if (mod.Equals(1))
                tach_mod = "00" + Convert.ToString(Num.ToString().Trim().Substring(0, 1)).Trim();
            if (mod.Equals(2))
                tach_mod = "0" + Convert.ToString(Num.ToString().Trim().Substring(0, 2)).Trim();
            if (mod.Equals(0))
                tach_mod = "000";
            // Tach hang con lai sau mod :
            if (Num.ToString().Length > 2)
                tach_conlai = Convert.ToString(Num.ToString().Trim().Substring(mod, Num.ToString().Length - mod)).Trim();

            ///don vi hang mod 
            int im = m + 1;
            if (mod > 0)
                lso_chu = Tach(tach_mod).ToString().Trim() + " " + Donvi(im.ToString().Trim());
            /// Tach 3 trong tach_conlai

            int i = m;
            int _m = m;
            int j = 1;
            string tach3 = "";
            string tach3_ = "";

            while (i > 0)
            {
                tach3 = tach_conlai.Trim().Substring(0, 3).Trim();
                tach3_ = tach3;
                lso_chu = lso_chu.Trim() + " " + Tach(tach3.Trim()).Trim();
                m = _m + 1 - j;
                if (!tach3_.Equals("000"))
                    lso_chu = lso_chu.Trim() + " " + Donvi(m.ToString().Trim()).Trim();
                tach_conlai = tach_conlai.Trim().Substring(3, tach_conlai.Trim().Length - 3);

                i = i - 1;
                j = j + 1;
            }
            if (lso_chu.Trim().Substring(0, 1).Equals("k"))
                lso_chu = lso_chu.Trim().Substring(10, lso_chu.Trim().Length - 10).Trim();
            if (lso_chu.Trim().Substring(0, 1).Equals("l"))
                lso_chu = lso_chu.Trim().Substring(2, lso_chu.Trim().Length - 2).Trim();
            if (lso_chu.Trim().Length > 0)
                lso_chu = dau.Trim() + " " + lso_chu.Trim().Substring(0, 1).Trim().ToUpper() + lso_chu.Trim().Substring(1, lso_chu.Trim().Length - 1).Trim() + " đồng chẵn.";

            return lso_chu.ToString().Trim();

        }
        #endregion
                
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (gv_chitiethangtra.Rows.Count == 0)
            {
                MessageBox.Show("Chưa có mặt hàng trả. Vui lòng nhập vào măt hàng trả.", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                try
                {
                    //them phieu tra hang
                    string manv = frmDangNhap.maNV_FromDangNhap;
                    string makhth = xulyma();
                    string makho = cmbKho.Text;
                    string makh = "KH0000";
                    if (rdb_hdbansi.Checked == true)
                    {
                        makh = cmbBanHang.EditorControl.Rows[cmbBanHang.SelectedIndex].Cells["MaKH"].Value.ToString();
                    }
                    string nguoitra = txtNguoiTra.Text;
                    string masothue = txtMST.Text;
                    string diachi = txtDiachi.Text;
                    DateTime ngaytra = rtpNgayTra.Value;
                    string chungtutra = txtChungtu.Text;
                    string ghichuth = txtGhiChu.Text;
                    double tongthanhtien = double.Parse(txtTongThanhTien.Text);
                    if (!service.InsertPhieuKHTraHang(makhth, manv, makho, makh, nguoitra, masothue, diachi, ngaytra, chungtutra, ghichuth, tongthanhtien))
                    {
                        MessageBox.Show("Lỗi khi lưu hoá đơn. Vui lòng liên hệ với phòng kĩ thuật.", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    //them chi tiet tra hang
                    for (int i = 0; i < gv_chitiethangtra.Rows.Count; i++)
                    {
                        string mahang = gv_chitiethangtra.Rows[i].Cells["mamh"].Value.ToString();
                        int soluong = int.Parse(gv_chitiethangtra.Rows[i].Cells["soluong"].Value.ToString());
                        double dongia = double.Parse(gv_chitiethangtra.Rows[i].Cells["dongia"].Value.ToString());
                        float khuyenmai = 0;
                        if (gv_chitiethangtra.Rows[i].Cells["ChiecKhau"].Value != null)
                            khuyenmai = float.Parse(gv_chitiethangtra.Rows[i].Cells["ChiecKhau"].Value.ToString());
                        double thanhtien = double.Parse(gv_chitiethangtra.Rows[i].Cells["thanhtien"].Value.ToString());
                        if (!service.InsertCTKHTraHang(mahang, makhth, soluong, dongia, khuyenmai, thanhtien))
                        {
                            MessageBox.Show("Lỗi khi lưu Chi tiết hoá đơn. Vui lòng liên hệ với phòng kĩ thuật.", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            gv_chitiethangtra.Rows[i].ErrorText = "Lỗi mặt hàng trả hàng. Vui lòng kiểm tra lại.";
                            return;
                        }
                    }
                    //Không Cập nhật lại công nợ khách hàng vì đã có Báo Cáo Công Nợ.

                    MessageBox.Show("Hóa đơn trả hàng của khách hàng đã được lưu.", "Thông Báo");

                    //Xuất Report nếu cần thiết.
                    if (check_in.Checked == true)
                    {
                        Report.frm_In_KHTraHang khth = new Report.frm_In_KHTraHang();
                        khth.MaKHTH = makhth;
                        khth.ShowDialog();
                    }

                    //reset cac bien
                    cmbBanHang.Text = "";
                    txtChungtu.Text = "";
                    cmbKho.Text = "";
                    cmbKhachHang.Text = "";
                    txtMST.Text = "";
                    txtNguoiTra.Text = "";
                    txtDiachi.Text = "";
                    txtTienhang.Text = "";
                    txtTienKhuyenMai.Text = "0";
                    txtMucThue.Text = "";
                    txtTienThue.Text = "";
                    txtTongThanhTien.Text = "";
                    txtTienChu.Text = "";
                    gv_chitiethangtra.DataSource = null;
                }
                catch (Exception ex)
                {
                    logger.Error("Error: " + ex.Message);
                    MessageBox.Show("Lỗi khi lưu hoá đơn. Vui lòng liên hệ với phòng kĩ thuật.", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
        }

        private bool ktmh(string mamh)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(service.selectqueryByText("select mamh from mathang where mamh like'" + mamh.Trim() + "'")));
            if (ds.Tables.Count > 0)
            {
                return true;
            }
            return false;
        }


        #region Load Form
        private void frmHangBanTraLai_Load(object sender, EventArgs e)
        {
            logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            try
            {
                rtpNgayTra.Value = DateTime.Today;
                //gan ten nhan vien
                txtnhanvien.Text = frmDangNhap.hoTenNV_FormDangNhap.ToString();

                //load thong hoa don ban si vao cmb
                Load_HDBL();                
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        private string xulyma()
        {
            DataSet ds_tam = new DataSet();
            string mamoi="";
            try
            {
                string maTam = "KHTH" + DateTime.Today.Year.ToString("0000") + DateTime.Today.Month.ToString("00");
                ds_tam.ReadXml(new StringReader(service.selectqueryByText("select top 1 MaKHTH from PhieuKHTraHang where MaKHTH like '%" + maTam + "%'order by MaKHTH desc")));
                if (ds_tam.Tables.Count == 0)
                    mamoi = maTam + "00001";
                else
                {
                    string matam = ds_tam.Tables[0].Rows[0][0].ToString();
                    string kituso = matam.Substring(10, 5).ToString();
                    int somoi = int.Parse(kituso) + 1;
                    mamoi = maTam + somoi.ToString("00000");
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
            return mamoi;
        }
        #endregion


        #region Bilding dữ liệu khi chọn một hóa đơn mua
        private void cmbBanHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {                
                if (cmbBanHang.Text != "")
                {
                    double tongTienTra_Tam = 0;
                    string mahdb = cmbBanHang.SelectedValue.ToString().Trim();
                    if (rdb_hdbanle.Checked == true)
                    {
                        
                        // load Hóa đơn mua
                        cmbKhachHang.Text = "";
                        cmbKho.Text = cmbBanHang.EditorControl.Rows[cmbBanHang.SelectedIndex].Cells["MaKho"].Value.ToString();
                        txtMST.Text = "";
                        txtNguoiTra.Text = "";
                        
                        txtMucThue.Text = cmbBanHang.EditorControl.Rows[cmbBanHang.SelectedIndex].Cells["VAT"].Value.ToString();
                        //txtTienhang.Text = double.Parse(cmbBanHang.EditorControl.Rows[cmbBanHang.SelectedIndex].Cells["TongThanhTien"].Value.ToString()).ToString("#,##0");
                        //load chi tiết hóa đơn mua
                        string sql1 = "select ct.MaMH, mh.TenMH,DonGia,SoLuong,ChietKhau,ThanhTien " +
                                            "from HoaDonBanLe hdbl, CTHDBanLe ct, MatHang mh " +
                                            "where hdbl.MaHDBL= ct.MaHDBL and mh.MaMH=ct.MaMH and ct.MaHDBL ='" + mahdb + "'";
                        DataSet ds_cthdbl = new DataSet();
                        ds_cthdbl.ReadXml(new StringReader(service.selectqueryByText(sql1)));
                        if (ds_cthdbl.Tables.Count > 0)
                        {
                            for (int i = 0; i < ds_cthdbl.Tables[0].Rows.Count; i++)
                            {                                
                                ds_cthdbl.Tables[0].Rows[i]["DonGia"] = double.Parse(ds_cthdbl.Tables[0].Rows[i]["DonGia"].ToString()).ToString("#,##0");
                                ds_cthdbl.Tables[0].Rows[i]["ThanhTien"] = double.Parse(ds_cthdbl.Tables[0].Rows[i]["ThanhTien"].ToString()).ToString("#,##0");
                            }
                            gv_chitiethangtra.DataSource = ds_cthdbl.Tables[0];
                            //Lay tong tien trong Gridview
                            if (gv_chitiethangtra.Rows.Count > 0)
                            {
                                for (int i = 0; i < gv_chitiethangtra.Rows.Count; i++)
                                {
                                    gv_chitiethangtra.Rows[i].Cells["STT"].Value = i + 1;
                                    tongTienTra_Tam += double.Parse(gv_chitiethangtra.Rows[i].Cells["ThanhTien"].Value.ToString());
                                }
                                txtTienhang.Text = tongTienTra_Tam.ToString("#,##0");
                            }
                        }
                        else
                        {
                            gv_chitiethangtra.DataSource = null;
                            txtTienChu.Text = "";
                            txtTienhang.Text = "";
                            txtTongThanhTien.Text = "";
                            txtMucThue.Text = "";
                            txtTienThue.Text = "";
                        }
                        if (gv_chitiethangtra.Rows.Count > 0)
                        {
                            //them một cột xóa
                            if (gv_chitiethangtra.Columns.GetColumnByFieldName("xoa").Length == 0)
                            {
                                GridViewCommandColumn btnColumn = new GridViewCommandColumn();
                                btnColumn.FieldName = "xoa";
                                btnColumn.Name = "xoa";
                                btnColumn.UseDefaultText = true;
                                btnColumn.DefaultText = "Xóa";
                                btnColumn.Width = 50;
                                gv_chitiethangtra.Columns.Insert(gv_chitiethangtra.Columns.Count, btnColumn);
                            }

                            if (txtMucThue.Text.Length != 0)
                            {
                                if (txtTienhang.Text.Length != 0)
                                {
                                    txtTienThue.Text = (double.Parse(txtTienhang.Text) * double.Parse(txtMucThue.Text) / 100).ToString("#,##0");
                                    txtTongThanhTien.Text = (double.Parse(txtTienhang.Text) - double.Parse(txtTienThue.Text)).ToString("#,##0");
                                    txtTienChu.Text = So_chu(double.Parse(txtTongThanhTien.Text));
                                }

                            }
                            else
                            {
                                txtTienThue.Text = "0";
                                txtTongThanhTien.Text = txtTienhang.Text;
                                txtTienChu.Text = So_chu(double.Parse(txtTongThanhTien.Text));
                            }
                        }

                    }
                    if (rdb_hdbansi.Checked == true)
                    {

                        //string mahdbs = cmbBanHang.SelectedValue.ToString().Trim();
                        // load Hóa đơn mua
                        cmbKhachHang.Text = cmbBanHang.EditorControl.Rows[cmbBanHang.SelectedIndex].Cells["HoTenKH"].Value.ToString();
                        cmbKho.Text = cmbBanHang.EditorControl.Rows[cmbBanHang.SelectedIndex].Cells["MaKho"].Value.ToString();
                        txtMST.Text = "";
                        txtNguoiTra.Text = cmbBanHang.EditorControl.Rows[cmbBanHang.SelectedIndex].Cells["HoTenKH"].Value.ToString(); 
                        txtDiachi.Text = cmbBanHang.EditorControl.Rows[cmbBanHang.SelectedIndex].Cells["DiaChiGiao"].Value.ToString();
                        txtGhiChu.Text = "";
                        txtMucThue.Text = cmbBanHang.EditorControl.Rows[cmbBanHang.SelectedIndex].Cells["VAT"].Value.ToString();
                        txtTienhang.Text = double.Parse(cmbBanHang.EditorControl.Rows[cmbBanHang.SelectedIndex].Cells["TongThanhTien"].Value.ToString()).ToString("#,##0");
                        //load chi tiết hóa đơn mua
                        string sql1 = "select ct.MaMH, mh.TenMH,  DonGia,SoLuong,ChietKhau, ThanhTien "
                                            + "from HoaDonBanSi hdbs, CTHDBanSi ct, MatHang mh "
                                            + "where hdbs.MaHDBS= ct.MaHDBS and mh.MaMH=ct.MaMH and ct.MaHDBS ='" + mahdb + "'";
                        DataSet ds_cthdbs = new DataSet();
                        ds_cthdbs.ReadXml(new StringReader(service.selectqueryByText(sql1)));
                        if (ds_cthdbs.Tables.Count > 0)
                        {
                            for (int i = 0; i < ds_cthdbs.Tables[0].Rows.Count; i++)
                            {
                                ds_cthdbs.Tables[0].Rows[i]["DonGia"] = double.Parse(ds_cthdbs.Tables[0].Rows[i]["DonGia"].ToString()).ToString("#,##0");
                                ds_cthdbs.Tables[0].Rows[i]["ThanhTien"] = double.Parse(ds_cthdbs.Tables[0].Rows[i]["ThanhTien"].ToString()).ToString("#,##0");
                            }                            
                            gv_chitiethangtra.DataSource = ds_cthdbs.Tables[0];
                            //Lấy tổng tiền hàng
                            if (gv_chitiethangtra.Rows.Count > 0)
                            {
                                for (int i = 0; i < gv_chitiethangtra.Rows.Count; i++)
                                {
                                    gv_chitiethangtra.Rows[i].Cells["STT"].Value = i + 1;
                                    tongTienTra_Tam += double.Parse(gv_chitiethangtra.Rows[i].Cells["ThanhTien"].Value.ToString());
                                }
                                txtTienhang.Text = tongTienTra_Tam.ToString("#,##0");
                            }
                        }
                        else
                        {
                            gv_chitiethangtra.DataSource = null;
                            txtTienChu.Text = "";
                            txtTienhang.Text = "";
                            txtTongThanhTien.Text = "";
                            txtMucThue.Text = "";
                            txtTienThue.Text = "";
                        }
                        if (gv_chitiethangtra.Rows.Count > 0)
                        {
                            //them một cột xóa
                            if (gv_chitiethangtra.Columns.GetColumnByFieldName("xoa").Length == 0)
                            {
                                GridViewCommandColumn btnColumn = new GridViewCommandColumn();
                                btnColumn.FieldName = "xoa";
                                btnColumn.Name = "xoa";
                                btnColumn.UseDefaultText = true;
                                btnColumn.DefaultText = "Xóa";
                                btnColumn.Width = 100;
                                gv_chitiethangtra.Columns.Insert(gv_chitiethangtra.Columns.Count, btnColumn);
                            }

                            if (txtMucThue.Text.Length != 0)
                            {
                                if (txtTienhang.Text.Length != 0)
                                {
                                    txtTienThue.Text = (double.Parse(txtTienhang.Text) * double.Parse(txtMucThue.Text) / 100).ToString("#,##0");
                                    txtTongThanhTien.Text = (double.Parse(txtTienhang.Text) - double.Parse(txtTienThue.Text)).ToString("#,##0");
                                    txtTienChu.Text = So_chu(double.Parse(txtTongThanhTien.Text));
                                }

                            }
                            else
                            {
                                txtTienThue.Text = "0";
                                txtTongThanhTien.Text = txtTienhang.Text;
                                txtTienChu.Text = So_chu(double.Parse(txtTongThanhTien.Text));
                            }
                        }
                    }
                }
                else
                {
                    gv_chitiethangtra.DataSource = null;
                    txtTienChu.Text = "";
                    txtTienhang.Text = "";
                    txtTongThanhTien.Text = "";
                    txtMucThue.Text = "";
                    txtTienThue.Text = "";
                    cmbKho.Text = "";
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }
        #endregion

        private void gv_chitiethangtra_CommandCellClick(object sender, EventArgs e)
        {
            double tongtien_sauxoa = 0;

            try
            {
                //string mamh = gv_chitiethangtra.CurrentRow.Cells["MaMH"].Value.ToString().Trim();
                //string mahd = cmbBanHang.SelectedValue.ToString().Trim();

                if (MessageBox.Show("Bạn có chắc chắn muốn hủy mặt hàng này của khách hàng?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (gv_chitiethangtra.Rows.Count == 1)
                    {
                        gv_chitiethangtra.Rows.Clear();
                    }
                    else
                    {
                        gv_chitiethangtra.Rows.RemoveAt(gv_chitiethangtra.CurrentRow.Index);
                        for (int i = 0; i < gv_chitiethangtra.Rows.Count; i++)
                        {
                            gv_chitiethangtra.Rows[i].Cells["STT"].Value = i + 1;
                        }
                    }
                    for (int j = 0; j < gv_chitiethangtra.Rows.Count; j++)
                    {
                        tongtien_sauxoa += double.Parse(gv_chitiethangtra.Rows[j].Cells["ThanhTien"].Value.ToString());
                    }
                    txtTienhang.Text = tongtien_sauxoa.ToString("#,##0");
                    if (txtMucThue.Text.Length != 0)
                    {
                        if (txtTienhang.Text.Length != 0)
                        {
                            txtTienThue.Text = (double.Parse(txtTienhang.Text) * double.Parse(txtMucThue.Text) / 100).ToString("#,##0");
                            txtTongThanhTien.Text = (double.Parse(txtTienhang.Text) - double.Parse(txtTienThue.Text)).ToString("#,##0");
                            txtTienChu.Text = So_chu(double.Parse(txtTongThanhTien.Text));
                        }

                    }
                    else
                    {
                        txtTienThue.Text = "0";
                        txtTongThanhTien.Text = txtTienhang.Text;
                        txtTienChu.Text = So_chu(double.Parse(txtTongThanhTien.Text));
                    }

                }

            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }

        #region load HÓa đơn Sỉ/Lẻ vào multicombobox
        private void rdb_hdbansi_CheckedChanged(object sender, EventArgs e)
        {
            if (rdb_hdbansi.Checked == true)
            {
                txtTienhang.Text = "0";
                txtMucThue.Text = "0";
                txtTienThue.Text = "";
                txtTongThanhTien.Text = "0";
                txtTienChu.Text = "Không chẵn";
                txtMST.Enabled = true;
                    Load_HDBS();
            }

        }
        private void rdb_hdbanle_CheckedChanged(object sender, EventArgs e)
        {
            if (rdb_hdbanle.Checked == true)
            {
                txtTienhang.Text = "0";
                txtMucThue.Text = "0";
                txtTienThue.Text = "";
                txtTongThanhTien.Text = "0";
                txtTienChu.Text = "Không chẵn";
                cmbKhachHang.Text = "";
                txtDiachi.Text = "";
                txtMST.Enabled = false;
                    Load_HDBL();

            }

        }
        private void Load_HDBL()
        {
            taosizeHDBL();
            DataSet ds = new DataSet();
            try
            {
                ds.ReadXml(new StringReader(service.selectqueryByText("select MaHDBL,MaKho,VAT,TongThanhTien  from HoaDonBanLe")));
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
            //---------------------//
            if (ds.Tables.Count > 0)
            {

                cmbBanHang.DataSource = ds.Tables[0];
                cmbBanHang.DisplayMember = "MaHDBL";
                cmbBanHang.ValueMember = "MaHDBL";
                cmbBanHang.SelectedIndex = -1;
            }
            FilterDescriptor des_dh = new FilterDescriptor(this.cmbBanHang.DisplayMember, FilterOperator.StartsWith, string.Empty);
            cmbBanHang.EditorControl.FilterDescriptors.Add(des_dh);
            cmbBanHang.Text = "";
        }
        private void Load_HDBS()
        {
            taosizeHDBS();
            DataSet ds = new DataSet();
            try
            {
                ds.ReadXml(new StringReader(service.selectqueryByText("select MaHDBS,MaKho,hd.MaKH,kh.HoTenKH,CongNoKH,DiaChiGiao,VAT,TongThanhTien,SoTienConLai from HoaDonBanSi hd, KhachHang kh where hd.MaKH= kh.MaKH")));
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
            //---------------------//
            if (ds.Tables.Count > 0)
            {
                cmbBanHang.DataSource = ds.Tables[0];
                cmbBanHang.DisplayMember = "MaHDBS";
                cmbBanHang.ValueMember = "MaHDBS";
                cmbBanHang.SelectedIndex = -1;
            }
            FilterDescriptor des_dh = new FilterDescriptor(this.cmbBanHang.DisplayMember, FilterOperator.StartsWith, string.Empty);
            cmbBanHang.EditorControl.FilterDescriptors.Add(des_dh);
            cmbBanHang.Text = "";
        }
        private void taosizeHDBS()
        {
            cmbBanHang.MultiColumnComboBoxElement.Columns.Clear();
            RadMultiColumnComboBoxElement multiColumnComboElement = this.cmbBanHang.MultiColumnComboBoxElement;
            multiColumnComboElement.DropDownSizingMode = SizingMode.UpDownAndRightBottom;
            multiColumnComboElement.DropDownMinSize = new Size(200, 100);
            GridViewTextBoxColumn mahdbl = new GridViewTextBoxColumn();
            mahdbl.HeaderText = "Mã HĐ";
            mahdbl.FieldName = "MaHDBS";
            mahdbl.Name = "MaHDBS";
            mahdbl.Width = 100;
            GridViewTextBoxColumn makho = new GridViewTextBoxColumn();
            makho.HeaderText = "Mã Kho";
            makho.FieldName = "MaKho";
            makho.Name = "MaKho";
            makho.IsVisible = false;
            makho.Width = 100;
            GridViewTextBoxColumn makhachhang = new GridViewTextBoxColumn();
            makhachhang.HeaderText = "Mã KH";
            makhachhang.FieldName = "MaKH";
            makhachhang.Name = "MaKH";
            makhachhang.IsVisible = false;
            makhachhang.Width = 100;
            GridViewTextBoxColumn tenkhachhang = new GridViewTextBoxColumn();
            tenkhachhang.HeaderText = "Tên KH";
            tenkhachhang.FieldName = "HoTenKH";
            tenkhachhang.Name = "HoTenKH";
            tenkhachhang.IsVisible = false;
            tenkhachhang.Width = 100;
            //GridViewTextBoxColumn nguoimua = new GridViewTextBoxColumn();
            //nguoimua.HeaderText = "Người Mua";
            //nguoimua.FieldName = "NguoiMua";
            //nguoimua.Name = "NguoiMua";
            //nguoimua.IsVisible = false;
            //nguoimua.Width = 100;
            //GridViewTextBoxColumn ngaybansi = new GridViewTextBoxColumn();
            //ngaybansi.HeaderText = "Ngày bán";
            //ngaybansi.FieldName = "NgayBanSi";
            //ngaybansi.Name = "NgayBanSi";
            //ngaybansi.IsVisible = false;
            //ngaybansi.Width = 100;
            //GridViewTextBoxColumn chungtubansi = new GridViewTextBoxColumn();
            //chungtubansi.HeaderText = "Chứng từ";
            //chungtubansi.FieldName = "ChungTuBanSi";
            //chungtubansi.Name = "ChungTuBanLe";
            //chungtubansi.IsVisible = false;
            //chungtubansi.Width = 100;
            GridViewTextBoxColumn diachi = new GridViewTextBoxColumn();
            diachi.HeaderText = "Địa chỉ giao";
            diachi.FieldName = "DiaChiGiao";
            diachi.Name = "DiaChiGiao";
            diachi.IsVisible = false;
            diachi.Width = 100;
            GridViewTextBoxColumn vat = new GridViewTextBoxColumn();
            vat.HeaderText = "VAT";
            vat.FieldName = "VAT";
            vat.Name = "VAT";
            vat.IsVisible = false;
            vat.Width = 100;
            GridViewTextBoxColumn tongthanhtien = new GridViewTextBoxColumn();
            tongthanhtien.HeaderText = "Tổng tiền";
            tongthanhtien.FieldName = "TongThanhTien";
            tongthanhtien.Name = "TongThanhTien";
            tongthanhtien.IsVisible = false;
            tongthanhtien.Width = 100;
            //GridViewTextBoxColumn sotiendatra = new GridViewTextBoxColumn();
            //sotiendatra.HeaderText = "Tiền đã trả";
            //sotiendatra.FieldName = "SoTienDaTra";
            //sotiendatra.Name = "SoTienDaTra";
            //sotiendatra.IsVisible = false;
            //sotiendatra.Width = 100;
            GridViewTextBoxColumn sotienconlai = new GridViewTextBoxColumn();
            sotienconlai.HeaderText = "Tiền còn lại";
            sotienconlai.FieldName = "SoTienConLai";
            sotienconlai.Name = "SoTienConLai";
            sotienconlai.IsVisible = false;
            sotienconlai.Width = 100;
            cmbBanHang.MultiColumnComboBoxElement.Columns.Add(mahdbl);
            cmbBanHang.MultiColumnComboBoxElement.Columns.Add(makho);
            cmbBanHang.MultiColumnComboBoxElement.Columns.Add(makhachhang);
            cmbBanHang.MultiColumnComboBoxElement.Columns.Add(tenkhachhang);
            //cmbBanHang.MultiColumnComboBoxElement.Columns.Add(ngaybansi);
            //cmbBanHang.MultiColumnComboBoxElement.Columns.Add(chungtubansi);
            cmbBanHang.MultiColumnComboBoxElement.Columns.Add(diachi);
            cmbBanHang.MultiColumnComboBoxElement.Columns.Add(vat);
            cmbBanHang.MultiColumnComboBoxElement.Columns.Add(tongthanhtien);
            cmbBanHang.MultiColumnComboBoxElement.Columns.Add(sotienconlai);
        }
        private void taosizeHDBL()
        {
            cmbBanHang.MultiColumnComboBoxElement.Columns.Clear();
            RadMultiColumnComboBoxElement multiColumnComboElement = this.cmbBanHang.MultiColumnComboBoxElement;
            multiColumnComboElement.DropDownSizingMode = SizingMode.UpDownAndRightBottom;
            multiColumnComboElement.DropDownMinSize = new Size(200, 100);
            GridViewTextBoxColumn mahdbl = new GridViewTextBoxColumn();
            mahdbl.HeaderText = "Mã HĐ";
            mahdbl.FieldName = "MaHDBL";
            mahdbl.Name = "MaHDBL";
            mahdbl.Width = 100;
            GridViewTextBoxColumn makho = new GridViewTextBoxColumn();
            makho.HeaderText = "Mã Kho";
            makho.FieldName = "MaKho";
            makho.Name = "MaKho";
            makho.IsVisible = false;
            makho.Width = 100;
            GridViewTextBoxColumn vat = new GridViewTextBoxColumn();
            vat.HeaderText = "VAT";
            vat.FieldName = "VAT";
            vat.Name = "VAT";
            vat.IsVisible = false;
            vat.Width = 100;
            GridViewTextBoxColumn tongthanhtien = new GridViewTextBoxColumn();
            tongthanhtien.HeaderText = "Tổng tiền";
            tongthanhtien.FieldName = "TongThanhTien";
            tongthanhtien.Name = "TongThanhTien";
            tongthanhtien.IsVisible = false;
            tongthanhtien.Width = 100;
            cmbBanHang.MultiColumnComboBoxElement.Columns.Add(mahdbl);
            cmbBanHang.MultiColumnComboBoxElement.Columns.Add(makho);
            cmbBanHang.MultiColumnComboBoxElement.Columns.Add(vat);
            cmbBanHang.MultiColumnComboBoxElement.Columns.Add(tongthanhtien);
        }
        #endregion 


        #region Khi thay doi so luong
        private void gv_chitiethangtra_CellValueChanged(object sender, GridViewCellEventArgs e)
        {
            try
            {
                string madh = cmbBanHang.SelectedValue.ToString();
                string mamh = gv_chitiethangtra.CurrentRow.Cells["MaMH"].Value.ToString().Trim();
                double thanhtien = double.Parse(gv_chitiethangtra.CurrentRow.Cells["ThanhTien"].Value.ToString());
                double dongia = double.Parse(gv_chitiethangtra.CurrentRow.Cells["DonGia"].Value.ToString());
                int soluong = int.Parse(gv_chitiethangtra.CurrentRow.Cells["Soluong"].Value.ToString().Trim());
                float khuyenmai = float.Parse(gv_chitiethangtra.CurrentRow.Cells["ChietKhau"].Value.ToString());

                thanhtien = (soluong*dongia) - ((soluong * dongia)*khuyenmai)/100;
                //gv_chitiethangtra.CurrentRow.Cells["ThanhTien"].Value = thanhtien;
            }
            catch (Exception ex)
            {
                logger.Error("Error: " + ex.Message);
            }
        }     
        private void gv_chitiethangtra_CellEndEdit(object sender, GridViewCellEventArgs e)
        {
            double tongtien_sausua=0;
            string madh = cmbBanHang.SelectedValue.ToString();
            string mamh = gv_chitiethangtra.CurrentRow.Cells["MaMH"].Value.ToString().Trim();
            double thanhtien = double.Parse(gv_chitiethangtra.CurrentRow.Cells["ThanhTien"].Value.ToString());
            double dongia = double.Parse(gv_chitiethangtra.CurrentRow.Cells["DonGia"].Value.ToString());
            int soluong = int.Parse(gv_chitiethangtra.CurrentRow.Cells["Soluong"].Value.ToString().Trim());
            float khuyenmai = float.Parse(gv_chitiethangtra.CurrentRow.Cells["ChietKhau"].Value.ToString());
            thanhtien = (soluong * dongia) - ((soluong * dongia) * khuyenmai) / 100;
            gv_chitiethangtra.CurrentRow.Cells["ThanhTien"].Value = thanhtien.ToString("#,##0");

            for (int j = 0; j < gv_chitiethangtra.Rows.Count; j++)
            {
                tongtien_sausua += double.Parse(gv_chitiethangtra.Rows[j].Cells["ThanhTien"].Value.ToString());
            }
            txtTienhang.Text = tongtien_sausua.ToString("#,##0");
            if (txtMucThue.Text.Length != 0)
            {
                if (txtTienhang.Text.Length != 0)
                {
                    txtTienThue.Text = (double.Parse(txtTienhang.Text) * double.Parse(txtMucThue.Text) / 100).ToString("#,##0");
                    txtTongThanhTien.Text = (double.Parse(txtTienhang.Text) - double.Parse(txtTienThue.Text)).ToString("#,##0");
                    txtTienChu.Text = So_chu(double.Parse(txtTongThanhTien.Text));
                }

            }
            else
            {
                txtTienThue.Text = "0";
                txtTongThanhTien.Text = txtTienhang.Text;
                txtTienChu.Text = So_chu(double.Parse(txtTongThanhTien.Text));
            }

        }
        #endregion


        #region Cac phuong thuc kok can
        private void txtSoluong_TextChanged(object sender, EventArgs e)
        {
            //if (txtSoluong.Text.Length != 0)
            //{
            //    double thanhtien = double.Parse(txtSoluong.Text) * double.Parse(txtDonGia.Text);
            //    txtThanhTien.Text = thanhtien.ToString("#,##0");
            //}
        }
        private void cmbTenHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            //    if (cmbTenHang.Text == "")
            //    {
            //        txtMaHang.Text = "";
            //        txtDonGia.Text = "0";
            //        txtSoluong.Text = "0";
            //        txtThanhTien.Text = "0";
            //        ddlDVT.Text = "";
            //    }
            //    else
            //    {
            //        DataSet ds_mathang = new DataSet();
            //        string mamh = cmbTenHang.SelectedValue.ToString().Trim();
            //        ds_mathang.ReadXml(new StringReader(service.selectqueryByText("select mamh,tenmh,dongiale,soluong,chietkhau,thanhtien,DVT from mathang where mamh='" + mamh + "'")));
            //        if (ds_mathang.Tables.Count > 0)
            //        {
            //            if (cmbTenHang.SelectedIndex!=-1)
            //            {
            //                txtMaHang.Text = ds_mathang.Tables[0].Rows[0][0].ToString();
            //                ddlDVT.Text = ds_mathang.Tables[0].Rows[0][6].ToString();
            //                txtDonGia.Text = int.Parse(ds_mathang.Tables[0].Rows[0][2].ToString()).ToString("#,##0");
            //                txtKhuyenMai.Text = ds_mathang.Tables[0].Rows[0][4].ToString();
            //            }

            //        }
            //    }
        }
        //private void btnChuyen_Click(object sender, EventArgs e)
        //{
        //    errorProvider1.Clear();
        //    if (txtMaHang.Text == "")
        //    {
        //        errorProvider1.SetError(txtMaHang, "Chưa chọn mặt hàng");
        //        return;
        //    }
        //    if (ktmh(txtMaHang.Text.Trim() + "") == false)
        //    {
        //        errorProvider1.SetError(txtMaHang, "Mặt hàng không hợp lệ");
        //        return;
        //    }
        //    if (txtSoluong.Text == "" || double.Parse(txtSoluong.Text) == 0)
        //    {
        //        errorProvider1.SetError(txtSoluong, "Đơn giá không hợp lệ");
        //        return;
        //    }
        //    if (txtDonGia.Text == "" || double.Parse(txtDonGia.Text) == 0)
        //    {
        //        errorProvider1.SetError(txtDonGia, "Đơn giá không hợp lệ");
        //        return;
        //    }
        //    try
        //    {
        //        //khai bao bien
        //        string tenhang = cmbTenHang.Text;
        //        string solg = txtSoluong.Text;
        //        string dongia = txtDonGia.Text;
        //        string khuyenmai = txtKhuyenMai.Text;
        //        string thanhtien = txtThanhTien.Text;
        //        //gan gia tri cho vitri
        //        if (gv_chitiethangtra.RowCount != 0)
        //            vitri = gv_chitiethangtra.RowCount;
        //        else
        //            vitri = 0;
        //        bool trungmh = true;
        //        for (int i = 0; i < gv_chitiethangtra.Rows.Count; i++)
        //        {
        //            if (cmbTenHang.SelectedValue.ToString().Trim() == gv_chitiethangtra.Rows[i].Cells["mamh"].Value.ToString().Trim())
        //            {
        //                gv_chitiethangtra.Rows[i].Cells["soluong"].Value = (int.Parse(gv_chitiethangtra.Rows[i].Cells["soluong"].Value.ToString()) + int.Parse(txtSoluong.Text.Trim())).ToString("#,##0");
        //                gv_chitiethangtra.Rows[i].Cells["thanhtien"].Value = int.Parse(gv_chitiethangtra.Rows[i].Cells["soluong"].Value.ToString()) * double.Parse(gv_chitiethangtra.Rows[i].Cells["dongia"].Value.ToString());
        //                gv_chitiethangtra.Rows[i].Cells["thanhtien"].Value = double.Parse(gv_chitiethangtra.Rows[i].Cells["thanhtien"].Value.ToString()).ToString("#,##0");
        //                if (i == gv_chitiethangtra.Rows.Count - 1)
        //                {
        //                    txtTongThanhTien.Text = double.Parse(gv_chitiethangtra.Rows[i].Cells["thanhtien"].Value.ToString()).ToString("#,##0");
        //                    tongtien = double.Parse(txtTongThanhTien.Text);
        //                }
        //                return;
        //            }
        //        }
        //        if (trungmh == true)
        //        {
        //            //tang stt len 1
        //            stt++;
        //            //them thong tin vao gridview
        //            gv_chitiethangtra.Rows.AddNew();
        //            gv_chitiethangtra.Rows[vitri].Cells["stt"].Value = stt;
        //            gv_chitiethangtra.Rows[vitri].Cells["MaMH"].Value = txtMaHang.Text;
        //            gv_chitiethangtra.Rows[vitri].Cells["TenMH"].Value = tenhang;
        //            gv_chitiethangtra.Rows[vitri].Cells["SoLuong"].Value = solg;
        //            gv_chitiethangtra.Rows[vitri].Cells["DonGia"].Value = dongia;
        //            gv_chitiethangtra.Rows[vitri].Cells["ChietKhau"].Value = khuyenmai;
        //            gv_chitiethangtra.Rows[vitri].Cells["ThanhTien"].Value = thanhtien;
        //        }
        //        //cap nhat lai tong tien hoa don
        //        tongtien = double.Parse(thanhtien);
        //        txtTienhang.Text = tongtien.ToString("#.##0");
        //        tongthanhtien += tongtien + double.Parse(txtTienThue.Text);
        //        txtTongThanhTien.Text = tongtien.ToString("#.##0");
        //        txtTienChu.Text = So_chu(double.Parse(txtTongThanhTien.Text));

        //        if (gv_chitiethangtra.Columns.GetColumnByFieldName("xoa").Length == 0)
        //        {
        //            GridViewCommandColumn btncolumn = new GridViewCommandColumn();
        //            btncolumn.FieldName = "xoa";
        //            btncolumn.Name = "xoa";
        //            btncolumn.UseDefaultText = true;
        //            btncolumn.DefaultText = "Xóa";
        //            gv_chitiethangtra.Columns.Insert(7, btncolumn);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        logger.Error("Error: " + ex.Message);
        //    }
        //}
        #endregion

        private void btn_timkiem_Click(object sender, EventArgs e)
        {
            frmBC_HangBanTraLai KHtrahang = new frmBC_HangBanTraLai();
            KHtrahang.Show();
        }

        private void btn_new_Click(object sender, EventArgs e)
        {
            if (rdb_hdbansi.Checked == true)
            {
                cmbKho.Text = "";
                cmbKhachHang.Text = "";
                txtDiachi.Text = "";
                txtGhiChu.Text = "";
                txtMST.Text = "";
                txtNguoiTra.Text = "";
                txtChungtu.Text = "";
                cmbBanHang.Text = "";

                txtMaHang.Text = "";
                txtSoluong.Text = "1";
                txtThanhTien.Text = "";
                txtDonGia.Text = "";
                cmbTenHang.Text = "";
                ddlDVT.Text = "";
                txtKhuyenMai.Text = "";

                txtTienhang.Text = "0";
                txtMucThue.Text = "0";
                txtTienThue.Text = "";
                txtTongThanhTien.Text = "0";
                txtTienChu.Text = "Không chẵn";
                txtMST.Enabled = true;
                Load_HDBS();
            }
            else if (rdb_hdbanle.Checked == true)
            {
                cmbKho.Text = "";
                txtGhiChu.Text = "";
                txtNguoiTra.Text = "";

                txtMaHang.Text = "";
                txtSoluong.Text = "1";
                txtThanhTien.Text = "";
                txtDonGia.Text = "";
                cmbTenHang.Text = "";
                ddlDVT.Text = "";
                txtKhuyenMai.Text = "";

                txtChungtu.Text = "";
                cmbBanHang.Text = "";
                txtTienhang.Text = "0";
                txtMucThue.Text = "0";
                txtTienThue.Text = "";
                txtTongThanhTien.Text = "0";
                txtTienChu.Text = "Không chẵn";
                cmbKhachHang.Text = "";
                txtDiachi.Text = "";
                txtMST.Enabled = false;
                Load_HDBL();

            }
        }

        private void btn_xemin_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = new DataSet();
                string makhth = "";
                ds.ReadXml(new StringReader(service.selectqueryByText("select top 1 MaKHTH from PhieuKHTraHang order by MaKHTH desc")));
                if (ds.Tables.Count > 0)
                {
                    makhth = ds.Tables[0].Rows[0][0].ToString();
                    Report.frm_In_KHTraHang khtrahang = new Report.frm_In_KHTraHang();
                    khtrahang.MaKHTH = makhth;
                    khtrahang.ShowDialog();
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }     

      
    }


}


   //try
   //             {
   //                 //them phieu tra hang
   //                 string manv = frmDangNhap.maNV_FromDangNhap;
   //                 string makhth = xulyma();
   //                 string makho = cmbKho.SelectedValue.ToString().Trim();
   //                 string makh = cmbKhachHang.SelectedValue.ToString().Trim();
   //                 string nguoitra = txtNguoiTra.Text;
   //                 string masothue = txtMST.Text;
   //                 string diachi = txtDiachi.Text;
   //                 DateTime ngaytra = rtpNgayTra.Value;
   //                 string chungtutra = txtChungtu.Text;
   //                 string ghichuth = txtGhiChu.Text;
   //                 double tongthanhtien = double.Parse(txtTongThanhTien.Text);
   //                 service.InsertPhieuKHTraHang(makhth, manv, makho, nguoitra, masothue, diachi, ngaytra, chungtutra, ghichuth, tongthanhtien);

   //                 //them chi tiet tra hang
   //                 for (int i = 0; i < gv_chitiethangtra.Rows.Count; i++)
   //                 {
   //                     string mahang = gv_chitiethangtra.Rows[i].Cells["mamh"].Value.ToString();
   //                     int soluong = int.Parse(gv_chitiethangtra.Rows[i].Cells["soluong"].Value.ToString());
   //                     double dongia = double.Parse(gv_chitiethangtra.Rows[i].Cells["dongia"].Value.ToString());
   //                     float khuyenmai = float.Parse(gv_chitiethangtra.Rows[i].Cells["chietkhau"].Value.ToString());
   //                     double thanhtien = double.Parse(gv_chitiethangtra.Rows[i].Cells["thanhtien"].Value.ToString());
   //                     if (service.InsertCTKHTraHang(mahang, makhth, soluong, dongia, khuyenmai, thanhtien) == true)
   //                     {
   //                         MessageBox.Show("Hoa Don Da Duoc Luu");
   //                     }
   //                 }
   //                 //reset cac bien
   //                 gv_chitiethangtra.Rows.Clear();
   //                 vitri = 0;

   //             }
   //             catch (Exception ex)
   //             {
   //                 logger.Error("Error: " + ex.Message);
   //             }